package com.example.asus.keshe;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class SqlLiteDemoActivity extends Activity implements View.OnClickListener{

    private static final String TAG = "SqlLiteDemoActivity";

    private Button button,button1,button2,button3;
    private EditText single;
    List<String> list;
    List<String> list1;

    private MySqliteHelper mySqlHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sql_lite);

        button=(Button)findViewById(R.id.button12);
        button2 = (Button)findViewById(R.id.button10);
        button3 = (Button)findViewById(R.id.button11);
        single=(EditText) findViewById(R.id.single);

        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button.setOnClickListener(this);

        mySqlHelper = new MySqliteHelper(this);
    }




    /**
     * API查询单个
     * @param id
     */
    public void query(Integer id){

        SQLiteDatabase db = mySqlHelper.getWritableDatabase();
        String[] args = {id.toString()};

        Cursor cursor = db.rawQuery("select _id,name,password,score from person where _id=?",args);
        if(cursor != null){
            if(cursor.moveToNext()){ //如果有查询结果
                int qId = cursor.getInt(cursor.getColumnIndex("_id"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String password = cursor.getString(cursor.getColumnIndex("password"));
                float score = cursor.getFloat(cursor.getColumnIndex("score"));
                Toast.makeText(this,qId+","+name+","+password+","+score,Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(this,id+"没有记录",Toast.LENGTH_LONG).show();
            }
            cursor.close();
        }
        db.close();
    }


    public void delete(String str){

        SQLiteDatabase db=mySqlHelper.getReadableDatabase();
        String[] args = {str};
        Cursor cursor = db.rawQuery("select _id,name,password,score from person where _id=?",args);
        if(cursor != null) {
            if(cursor.moveToNext()) { //如果有查询结果
                db.delete("person","_id="+str,null);
                Toast.makeText(this, "删除成功！", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this,str+"没有记录",Toast.LENGTH_LONG).show();

            }
            cursor.close();
        }
        db.close();
    }
    /**
     * 查询所有
     */
    public void queryAll(){
        SQLiteDatabase db = mySqlHelper.getWritableDatabase();
        Cursor cursor = db.query("person",null,null,null,null,null,null);

        if(cursor != null){
            //遍历游标
            for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                int qId = cursor.getInt(cursor.getColumnIndex("_id"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String password = cursor.getString(cursor.getColumnIndex("password"));
                float score = cursor.getFloat(cursor.getColumnIndex("score"));
                list.add(qId+","+name);
                list1.add(","+password+","+score);
                Toast.makeText(this, qId+","+name+","+password+","+score,Toast.LENGTH_SHORT).show();
            }
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.button10: //查询单条记录
                if(single.length()==0){
                    Toast.makeText(this, "输入框为空，请重新输入", Toast.LENGTH_SHORT).show();
                }else{
                    query(Integer.parseInt( single.getText().toString()));
                }

                break;

            case R.id.button11: //查询所有
                ListView listView = (ListView) this.findViewById(R.id.listView);
                SQLiteDatabase db = mySqlHelper.getWritableDatabase();
                Cursor cursor = db.query("person",null,null,null,null,null,null);
                list = new ArrayList<>();
                list1 = new ArrayList<>();
                if(cursor != null){
                    //遍历游标
                    for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                        int qId = cursor.getInt(cursor.getColumnIndex("_id"));
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String password = cursor.getString(cursor.getColumnIndex("password"));
                        float score = cursor.getFloat(cursor.getColumnIndex("score"));
                        list.add(qId+"      "+name+"       "+password+"       ");
                        list1.add("");
                    }
                }

                List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
                for(int i = 0; i < list .size(); i++){
                    HashMap<String, Object> item = new HashMap<String, Object>();
                    item.put("name", list.get(i));
                    item.put("sex", list1.get(i));
                    data.add(item);
                }
                SimpleAdapter adapter = new SimpleAdapter(SqlLiteDemoActivity.this, data, R.layout.item1,
                        new String[]{"name", "password"}, new int[]{R.id.name, R.id.password});
                listView.setAdapter(adapter);
                break;
           case R.id.button12:
               if(single.length()==0){
                   Toast.makeText(this, "输入框为空，请重新输入", Toast.LENGTH_SHORT).show();
               }else{

                   delete(single.getText().toString());
               }

               break;

        }
    }
}








